package com.eastfair.common.service.impl;

import cn.hutool.core.date.StopWatch;
import com.eastfair.common.dto.GmApiSvcInfOrganizationDtos;
import com.eastfair.common.dto.GmApiSvcInfOrganizationUserDtos;
import com.eastfair.common.dto.GmApiSvcInfPageDtos;
import com.eastfair.common.res.CrmResult;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.common.service.CrmSvcInfSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Crm服务商同步 service.
 *
 * @author dqq
 * @date 2023 /2/13
 */
@Service
@Slf4j
public class CrmSvcInfSyncServiceImpl implements CrmSvcInfSyncService {

    @Value("${roma.ipport}")
    private String romaIpport;

    @Value("${roma.CRM-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.CRM-X-HW-APPKEY}")
    private String romaXHWAPPKEY;

    @Value("${crm.customer_provider.url}")
    private String crmCustomerProviderUrl;


    //同步组织信息
    @Value("${crm.contact_organization.url}")
    private String contactOrganizationUrl;

    //同步组织和人信息
    @Value("${crm.contact_organization_user.url}")
    private String contactOrganizationUserUrl;

    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;

    @Override
    public CrmResult syncSvcInfo(GmApiSvcInfPageDtos gmApiSvcInfPageDtos) {
        log.info("syncSvcInfo - 同步Crm服务商, gmApiSvcInfPageDtos={}", gmApiSvcInfPageDtos);
        String httpUrl = romaIpport + crmCustomerProviderUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncServiceProvider");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiSvcInfPageDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncSvcInfo - 同步Crm服务商完成, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public void syncOrganizationInfo(GmApiSvcInfOrganizationDtos gmApiSvcInfOrganizationDtos) {
        String httpUrl = romaIpport + contactOrganizationUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncOrganization");
        log.info("===================================syncServiceProvider start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, gmApiSvcInfOrganizationDtos, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncServiceProvider end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
    }


    @Override
    public void syncOrganizationUserInfo(GmApiSvcInfOrganizationUserDtos gmApiSvcInfOrganizationUserDtos) {
        String httpUrl = romaIpport + contactOrganizationUserUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncOrganization");
        log.info("===================================syncServiceProvider start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, gmApiSvcInfOrganizationUserDtos, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncServiceProvider end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
    }


}